#ifndef QNBLOGIN_H
#define QNBLOGIN_H

#include <QDialog>
#include <QCamera>
#include <QCameraViewfinder>
#include <QCameraImageCapture>
//added by xuhongneng @20170825 begin
#include <QGraphicsVideoItem>
//added by xuhongneng @#@1070825 end
#include <QGraphicsView>
#include <QFileDialog>


#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QNetworkReply>
#include <QTranslator>
#include <QMessageBox>

#include <QProcess>

#include "QNBHeartThread.h"
#include "QNBLockCtrlAltDel.h"
#include "QNBCaptureVideoFrame.h"

#include "QNBExCamera.h"

namespace Ui {
class QNBLogin;
}


class QCamera;
class QCameraViewfinder;
class QCameraImageCapture;
//added by xuhongneng @20170825 begin
class QGraphicsVideoItem;
class QGraphicsScene;
class QGraphicsView;
//added by xuhongneng @20170825 end


typedef HWND (WINAPI *PROCGETTASKMANWND)(void);

class QNBLogin : public QDialog
{
    Q_OBJECT

public:
    explicit QNBLogin(QWidget *parent = 0);
    ~QNBLogin();


    static QNBLogin* getInstance();
    static void clearResource();
    void keepTopWindowsInit();
    void keepTopWindows(bool isShow);

private slots:

    void loginReqAndCheck();
    void modifyId();
    void replyFinished(QNetworkReply *reply);
    void reqLoginError(QNetworkReply::NetworkError errorCode);
    void reqCheckPhotoSimilarError(QNetworkReply::NetworkError errorCode);
    void reqSubmitPhotoError(QNetworkReply::NetworkError errorCode);
    void reqGetAuthPhotoError(QNetworkReply::NetworkError errorCode);
    void printCameraError(QCamera::Error errorCode);

    void replySaveAuthPhotoFinished(QNetworkReply *reply);
    void replyCheckPhotoFinished(QNetworkReply *reply);

    void captureImage();
    void saveImage(int , QImage image);
#ifdef _WINXP
    void saveImage(QImage image);
#endif

    void submitPhotoByInterval();
    void checkNetBarDaemonExist();
    void checkPhotoByInterval();
    void checkRunningStatus();

#ifdef _WINXP
    void captureImageToVideo();
#endif

    void replySubmitPhotoFinished(QNetworkReply *reply);

    void readCheckPhotoResult(int exitCode);

    bool login();
    void logout();


private:
    bool analyzeLoginResp(QByteArray data);
    void reqAuthPhotoFile();
    bool checkPhoto();


    void deleteUserData();
    void InitUiByLanguage(const QString strLanguage);

    QByteArray getPhotoBase64(const QString& filepath);
    bool savePhotoBase64(QByteArray arr, const QString& filepath);
    void runCheckPhotoExe();

    void checkTimer(QTimer *timer, int interval);
private:
    Ui::QNBLogin *ui;
    QTranslator  *m_translator;

    QNetworkAccessManager *reqLogin;
    QNetworkAccessManager *reqSaveAuthPhoto;
    QNetworkReply *reply;
    QNetworkReply *reply2;


    QNetworkAccessManager *reqCheckPhoto;
    QNetworkReply *reply3;


    QNetworkAccessManager *reqSubmitPhoto;
    QNetworkReply *reply4;

#ifdef _WINXP
    QNBExCamera *m_pCamera;
    QTimer *m_pCaptureToVideoTimer;
#else
    QCamera *camera;
    //QCameraViewfinder *viewfinder;
    QCameraImageCapture *viewfinder;
    CCaptureVideoFrame *m_pCaptureVideoFrame;
    QCameraImageCapture *imageCapture;
#endif

    //added by xuhongneng @20170825 begin
    QGraphicsVideoItem *m_pVideoItem;
    QGraphicsScene *m_pScence;
    QGraphicsView *m_pGraphicsView;
    //added by xuhongneng @20170825 end

    QNBHeartThread *m_heart;

    QProcess *m_pCheckPhotoExe;

    //身份证
    QString mstrIdentify;


    bool bCheckPhotoRunning;
    bool m_bVerifying;

    //抓图实时路径
    QString mstrCapPhoto;

    //定时比对图片
    QTimer *m_pCheckPhotoByIntervalTimer;

    //定时比对图
    QTimer *m_pSubmitPhotoByIntervalTimer;

    //定时轮询检查系统运行状态
    QTimer *m_pCheckStatusTimer;

    //added by xuhongneng @20170814 begin
    QTimer *m_pCheckDaemonProcessTimer;
    //added by xuhongneng @#20170814 end


    QMessageBox loginMsgBox;
    QMessageBox serverErrorMsgBox;
    QMessageBox checkPhotoErrorMsgBox;
    QMessageBox msgBox;


    HWND hTaslManWnd;
    PROCGETTASKMANWND  GetTaskmanWindow;

    static QNBLogin *m_sgLogin;
};

#endif // QNBLOGIN_H
